/**
 * Created on Dec 10, 2006
 */
package info.niwota.js;

import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import uk.ltd.getahead.dwr.util.JavascriptUtil;

/**
 * @author qiangli
 * 
 */
public class DWRCompressor extends BaseCompressor implements Compressor {

	public DWRCompressor() {
		super();
	}

//	private String read(Reader r) throws Exception {
//		BufferedReader in = null;
//		StringBuffer sb = new StringBuffer();
//		try {
//			in = new BufferedReader(r, BUFSIZE);
//			String str = null;
//			while ((str = in.readLine()) != null) {
//				sb.append(str);
//				sb.append("\r\n");
//			}
//		} finally {
//			//
//		}
//		return sb.toString();
//	}

	private void write(Writer w, String str) throws Exception {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(w, BUFSIZE);
			out.write(str);
		} finally {
			//
		}
	}
//
//	public void compress(File in, File out) throws Exception {
//		FileReader is = null;
//		FileWriter os = null;
//		try {
//			is = new FileReader(in);
//			os = new FileWriter(out);
//			JavascriptUtil jsu = new JavascriptUtil();
//			String otext = read(is);
//			String ctext = jsu.compress(otext, JavascriptUtil.LEVEL_DEBUGGABLE
//					| JavascriptUtil.COMPRESS_STRIP_BLANKLINES);
//			write(os, ctext);
//		} finally {
//			close(is);
//			close(os);
//		}
//	}
	
	public void compress(String script, OutputStream out) throws Exception {
		try {
			JavascriptUtil jsu = new JavascriptUtil();
			String ctext = jsu.compress(script, JavascriptUtil.LEVEL_DEBUGGABLE
					| JavascriptUtil.COMPRESS_STRIP_BLANKLINES);
			write(new OutputStreamWriter(out), ctext);
		} finally {
			//
		}
	}
}
